<?php

namespace App\Controller\Admin;

use App\Controller\ApiController;
use App\Logic\Admin\AdministratorLogic;
use Hyperf\Di\Annotation\Inject;
use Hyperf\Logger\LoggerFactory;
use Psr\Http\Message\ResponseInterface;
use Throwable;

class AdministratorController extends ApiController
{
    #[Inject]
    protected AdministratorLogic $administratorLogic;
    public function __construct(LoggerFactory $loggerFactory)
    {
        parent::__construct($loggerFactory);
        $this->administratorLogic = new AdministratorLogic();
    }

    /**
     * Notes:获取当前管理员的信息
     * 2025/7/9
     * @return ResponseInterface
     * @throws \Exception
     */
    public function administrator(): ResponseInterface
    {
        try {
            $admin = $this->request->getAttribute('admin',[]);
            $admin = $this->administratorLogic->administrator($admin);
            return $this->responseSuccess($admin);
        }catch (Throwable $throwable){
            return $this->responseError($throwable->getCode(),$throwable->getLine(),$throwable->getFile() ,$throwable->getMessage());
        }
    }

    /**
     * Notes:列表
     * 2025/7/15
     * @return ResponseInterface
     * @throws \Exception
     */
    public function list(): ResponseInterface
    {
        try {
            $params = $this->request->all();
            $result = $this->administratorLogic->list($params);
            return $this->responseSuccess($result);
        }catch (Throwable $throwable){
            return $this->responseError($throwable->getCode(),$throwable->getLine(),$throwable->getFile() ,$throwable->getMessage());
        }
    }

    /**
     * Notes:退出登录
     * 2025/7/17
     * @return ResponseInterface
     * @throws \Exception
     */
    public function logout(): ResponseInterface
    {
        try {
            $token = $this->request->header("X-Token");
            $result = $this->administratorLogic->logout($token);
            return $this->responseSuccess($result);
        }catch (Throwable $throwable){
            return $this->responseError($throwable->getCode(),$throwable->getLine(),$throwable->getFile() ,$throwable->getMessage());
        }
    }

    /**
     * Note:获取当前管理员的信息
     * User: Kong
     * Date: 2025/9/25
     * Time: 14:54
     * @return ResponseInterface
     * @throws \Exception
     */
    public function getCurrentAdministratorInfo(): ResponseInterface
    {
        try {
            $token = $this->request->header("X-Token");
            $result = $this->administratorLogic->getCurrentAdministratorInfo($token);
            return $this->responseSuccess($result);
        }catch (Throwable $throwable){
            return $this->responseError($throwable->getCode(),$throwable->getLine(),$throwable->getFile() ,$throwable->getMessage());
        }
    }

    /**
     * Note:创建管理员
     * User: Kong
     * Date: 2025/9/25
     * Time: 14:55
     * @return ResponseInterface
     * @throws \Exception
     */
    public function create(): ResponseInterface
    {
        try {
            $result = $this->administratorLogic->create($this->request->all());
            return $this->responseSuccess($result);
        }catch (Throwable $throwable){
            return $this->responseError($throwable->getCode(),$throwable->getLine(),$throwable->getFile() ,$throwable->getMessage());
        }
    }

    /**
     * Note:获取管理员的信息
     * User: Kong
     * Date: 2025/9/25
     * Time: 15:22
     * @return ResponseInterface
     * @throws \Exception
     */
    public function getAdministratorInfo(): ResponseInterface
    {
        try {
            $result = $this->administratorLogic->getAdministratorInfo($this->request->all());
            return $this->responseSuccess($result);
        }catch (Throwable $throwable){
            return $this->responseError($throwable->getCode(),$throwable->getLine(),$throwable->getFile() ,$throwable->getMessage());
        }
    }

    /**
     * Note:修改管理员信息
     * User: Kong
     * Date: 2025/9/25
     * Time: 15:25
     * @return ResponseInterface
     * @throws \Exception
     */
    public function update(): ResponseInterface
    {
        try {
            $result = $this->administratorLogic->update($this->request->all());
            return $this->responseSuccess($result);
        }catch (Throwable $throwable){
            return $this->responseError($throwable->getCode(),$throwable->getLine(),$throwable->getFile() ,$throwable->getMessage());
        }
    }

    /**
     * Note:修改管理员密码
     * User: Kong
     * Date: 2025/9/25
     * Time: 15:25
     * @return ResponseInterface
     * @throws \Exception
     */
    public function updatePassword(): ResponseInterface
    {
        try {
            $result = $this->administratorLogic->updatePassword($this->request->all());
            return $this->responseSuccess($result);
        }catch (Throwable $throwable){
            return $this->responseError($throwable->getCode(),$throwable->getLine(),$throwable->getFile() ,$throwable->getMessage());
        }
    }

    /**
     * Note:封禁用户
     * User: Kong
     * Date: 2025/9/25
     * Time: 15:25
     * @return ResponseInterface
     * @throws \Exception
     */
    public function forbid(): ResponseInterface
    {
        try {
            $result = $this->administratorLogic->forbid($this->request->all());
            return $this->responseSuccess($result);
        }catch (Throwable $throwable){
            return $this->responseError($throwable->getCode(),$throwable->getLine(),$throwable->getFile() ,$throwable->getMessage());
        }
    }

    /**
     * Note:删除用户
     * User: Kong
     * Date: 2025/9/25
     * Time: 15:26
     * @return ResponseInterface
     * @throws \Exception
     */
    public function destroy(): ResponseInterface
    {
        try {
            $result = $this->administratorLogic->destroy($this->request->all());
            return $this->responseSuccess($result);
        }catch (Throwable $throwable){
            return $this->responseError($throwable->getCode(),$throwable->getLine(),$throwable->getFile() ,$throwable->getMessage());
        }
    }

    /**
     * Note:设置管理员的角色
     * User: Kong
     * Date: 2025/9/25
     * Time: 15:26
     * @return ResponseInterface
     * @throws \Exception
     */
    public function setAdminRole(): ResponseInterface
    {
        try {
            $result = $this->administratorLogic->setAdminRole($this->request->all());
            return $this->responseSuccess($result);
        }catch (Throwable $throwable){
            return $this->responseError($throwable->getCode(),$throwable->getLine(),$throwable->getFile() ,$throwable->getMessage());
        }
    }
}